/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.meanzoft.m3d.model.mdx;

import org.lwjgl.util.vector.Vector2f;
import org.lwjgl.util.vector.Vector3f;
import org.meanzoft.m3d.model.m3dmodel.Extent;

/**
 *
 * @author Meanz
 */
public class GeoSetParser {

    private GeoSet geoSet;

    public GeoSetParser(GeoSet geoSet) {
        this.geoSet = geoSet;
    }

    /**
     * VRTX
     * @param buffer 
     */
    public void parseVertexData(ByteStream buffer) {
        int numVertices = buffer.getLEDWORD();

        for (int i = 0; i < numVertices; i++) {
            Vector3f vertex = new Vector3f(buffer.getFloat(), buffer.getFloat(), buffer.getFloat());
            geoSet.vertexList.addFirst(vertex);
        }
    }

    /**
     * NRMS
     * @param buffer 
     */
    public void parseNormalData(ByteStream buffer) {
        int numNormals = buffer.getLEDWORD();

        for (int i = 0; i < numNormals; i++) {
            Vector3f normal = new Vector3f(buffer.getFloat(), buffer.getFloat(), buffer.getFloat());
            geoSet.normalList.addFirst(normal);
        }
    }

    /**
     * PTYP
     * @param buffer 
     */
    public void parseFaceGroupTypeData(ByteStream buffer) {
        int numFaceTypeGroups = buffer.getLEDWORD();

        for (int i = 0; i < numFaceTypeGroups; i++) {
            int faceType = buffer.getDWORD();
            if (faceType != 4) {
                System.err.println("Amigod cul face");
            }
            /**
             * 4 - GL_TRIANGLES
             * ? - GL_TRIANGLE_STRIP
             * ? - GL_TRIANGLE_FAN
             * ? - GL_QUADS
             * ? - GL_QUAD_STRIP
             */
        }
    }

    /**
     * PCNT
     * @param buffer 
     */
    public void parseFaceGroupData(ByteStream buffer) {
        int numFaceGroups = buffer.getDWORD();

        for (int i = 0; i < numFaceGroups; i++) {
            int x = buffer.getDWORD();
        }
    }

    /**
     * PVTX
     * @param buffer 
     */
    public void parseFaceData(ByteStream buffer) {
        int numIndexes = buffer.getLEDWORD();
        int numFaces = numIndexes / 3;
        for (int i = 0; i < numFaces; i++) {
            Integer[] face = new Integer[3];
            face[0] = buffer.getWord();
            face[1] = buffer.getWord();
            face[2] = buffer.getWord();
            geoSet.faceList.add(new Vector3f(face[0], face[1], face[2]));
        }
    }

    /**
     * GNDX
     * @param buffer 
     */
    public void parseVertexGroupData(ByteStream buffer) {
        int numVertexGroups = buffer.getDWORD();

        for (int i = 0; i < numVertexGroups; i++) {
            char groupChar = (char) buffer.get();
            geoSet.groupList.add("" + groupChar);
        }
    }

    /**
     * MTGC
     * @param buffer
     */
    public void parseMTGC(ByteStream buffer) {
        int numMTGC = buffer.getDWORD();

        for (int i = 0; i < numMTGC; i++) {
            int matrixListSize = buffer.getLEDWORD();
            GeoSetGroup gsg = new GeoSetGroup();
            gsg.matrixListSize = matrixListSize;
            geoSet.matrixList.add(gsg);
        }
    }

    /**
     * Mats
     * @param buffer 
     */
    public void parseMatriceData(ByteStream buffer) {
        int numMatrices = buffer.getDWORD();

        for (int i = 0; i < numMatrices; i++) {
            //new group node assign
            int nodeId = buffer.getDWORD();
        }

        //geoset->
        int materialId = buffer.getDWORD();
        int selectionGroup = buffer.getDWORD();
        boolean unselectable = buffer.getDWORD() == 4;

        //geoset->extent
        float radius = buffer.getFloat();
        Vector3f min = new Vector3f(buffer.getFloat(), buffer.getFloat(), buffer.getFloat());
        Vector3f max = new Vector3f(buffer.getFloat(), buffer.getFloat(), buffer.getFloat());
        Extent extent = new Extent(radius, min, max);

        int numAnimations = buffer.getDWORD();
        for (int i = 0; i < numAnimations; i++) {
            //new extend
            float _radius = buffer.getFloat();
            Vector3f _min = new Vector3f(buffer.getFloat(), buffer.getFloat(), buffer.getFloat());
            Vector3f _max = new Vector3f(buffer.getFloat(), buffer.getFloat(), buffer.getFloat());
            Extent _extent = new Extent(_radius, _min, _max);
            //geoSet->addExtent();
        }
    }

    /**
     * UVAS
     * @param buffer
     */
    public void parseTextureVertexGroups(ByteStream buffer) {
        int numTexVertexGroups = buffer.getDWORD();
    }

    public void parseTextureCoordinateData(ByteStream buffer) {
        int numTexCoords = buffer.getDWORD();

        for (int i = 0; i < numTexCoords; i++) {
            Vector2f textureCoord = new Vector2f(buffer.getFloat(), buffer.getFloat());
            geoSet.texCoords.add(textureCoord);
        }

    }
}
